package simulation;


public class Road {
	public static int[][] roadArray=new int[2][5]; // [lane][segment]
    int blockSize = 50;
    private Object right;
    private Object set;
    private Object left;

    
	Road(){
		//roadArray = new int[2][5]; 
		right = new Object();
		set = new Object();
		left = new Object();
	}

        public  void setPosition(int id,int lane, int segment){
        	synchronized(set){
        		while(!isPositionAvailable(id,lane,segment)){
        			try {
						set.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
        		}
    		}           
        }
        
        private synchronized boolean isPositionAvailable(int id,int lane,int segment){
        	if(roadArray[lane][segment]!=1){
				roadArray[lane][segment]=1;
				System.out.println("			Car"+id+" setPosition at ("+lane+" "+segment+")");
				return true;
			}
			else {
				System.out.println("			Car "+id+" Waiting! Already ("+lane+" "+segment+") set to 1");
				return false;
			}
        }
        
	public boolean rightAvailable(int id,int lane, int segment) {
		
		synchronized(right){
			
			if(isRightAvailable(id,lane,segment))
				return true;
			else
				return false;
			
		/*	while(!isRightAvailable(id,lane,segment)){
    			try {
    				System.out.println("Car "+id+" Waiting!");
					right.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
    		}*/
			
		}
	}
	
	
	 private synchronized boolean isRightAvailable(int id,int lane,int segment){
     	
		 if(roadArray[1][segment+1]==0){
				//roadArray[1][segment+1]=1;
				//resetPosition(id,lane,segment);
				//System.out.println("Car "+id+" In right lane! Car is at (1 "+(segment+1)+")");
				return true;
			}
			else {
				//System.out.println("Car "+id+" Checking for left availability");
				//if(isLeftAvailable(id,lane,segment))
					//return true;
				//else
					return false;
			}
     }

	 public  void leftAvailable(int id,int lane, int segment) {
			
			synchronized(left){
				while(!isLeftAvailable(id,lane,segment)){
	    			try {
						left.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
	    		}
				
			}
		}
	 	 
	 private synchronized boolean isLeftAvailable(int id,int lane,int segment) {
		if(roadArray[0][segment+1]==0){
			//roadArray[0][segment+1]=1;
			//resetPosition(id,lane,segment);
			//System.out.println("Car "+id+" In left lane! Car is at (0 "+(segment+1)+")");
			return true;
		}
		else {
			System.out.println("Car "+id+" Waiting! Already (0 "+(segment+1)+") set to 1");
			return false;
		}
	}

	public void resetPosition(int id,int lane,int segment){
		
		synchronized(this){
		if(roadArray[lane][segment]==1){
			roadArray[lane][segment]=0;
			System.out.println("Car "+id+" has RESET ("+lane+" "+segment+") to 0");
		}
		else
			System.out.println("Car "+id+" COULD NOT RESET ("+lane+" "+segment+") to 0");		
		}
		
		synchronized(set){
			set.notifyAll();
		}
		synchronized(left){
			left.notifyAll();
		}
		
	}
	 
	 
/*	public  boolean isFrontAvailable(int lane,int segment) {
		//if (roadArray[lane][segment+1]==1)
	/*	if(WTS[lane][segment]>WTS[lane][segment+1])
			return true;
		else 
			return false;
		// TODO Auto-generated method stub
	}*/
	
}
